﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BibleService.Lib
{
	/// <summary>
	/// 章管理
	/// </summary>
	public class Chapter
	{
		public int ChapterID{get;set;}
		public int ChapterNumber{get;set;}
		public string ChapterName{get;set;}
		public int BookID{get;set;}
		public string BookName { get; set; }
		public String Mp3Url{get;set;}
		
		/// <summary>
		/// 添加章
		/// </summary>
		/// <returns></returns>
		public Chapter Add()
		{
			string insertChapterSql = string.Format("Insert Into Chapter(ChapterNumber,BookID,BookName,ChapterName,Mp3Url) values('{0}','{1}','{2}','{3}','{4}')",
								this.ChapterNumber,
								this.BookID,
								this.BookName,
								this.ChapterName,
								this.Mp3Url);
			Lib._SqlHelper.Execute<Int32>(insertChapterSql);
			this.ChapterID = (Int32)Lib._SqlHelper.GetSingCell<Int64>("select max(ChapterID) from Chapter");
			return this;
		}
		/// <summary>
		/// 获取指定篇的章列表
		/// </summary>
		/// <param name="BookID"></param>
		/// <param name="ChapterNumber"></param>
		/// <returns></returns>
		public static List<Chapter> GetChapterList(Int32 BookID)
		{
			string selectSql = string.Format("select ChapterID,ChapterNumber,BookID,BookName,ChapterName,Mp3Url from Chapter where BookID='{0}' ", BookID);
			
			List<Chapter> list=new List<Chapter>();
			using(var dataReader=_SqlHelper.GetList(selectSql))
			{
				while(dataReader.Read())
				{
					var model=new Lib.Chapter();
					model.ChapterID = dataReader.GetInt32(0);
					model.ChapterNumber=dataReader.GetInt32(1);
					model.BookID = dataReader.GetInt32(2);
					model.BookName = dataReader.GetString(3);
					model.ChapterName = dataReader.GetString(4);
					//string _urlString = dataReader.GetString(5);
					//if(string.IsNullOrEmpty(_urlString)==false)
					//	model.Mp3Url = new Uri(_urlString);
					model.Mp3Url = dataReader.GetString(5);
					list.Add(model);
				}
			}
			return list;
		}
	}
}
